<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" />
	<title>
    <?php
if (isset($_COOKIE['user'])) {
    echo $_COOKIE['user'] . "的主页";
}
?>
	</title>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="css/homepage.css">
	<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
	<script type="text/javascript" src="js/cookie.js"></script>
	<script type="text/javascript" src="js/homepage.js"></script>

</head>
<?php
if (!isset($_COOKIE['user'])) {
    echo "<script>alert('请您登录');window.location.href='index.php';</script>";
}
echo $_COOKIE['user'] . "的主页";
?>
<body>
	<div class="skin"></div>
	<div id="bk1" class="bk">色</div>
	<div id="bk2" class="bk">颜</div>
	<div id="bk3" class="bk">的</div>
	<div id="bk4" class="bk">欢</div>
	<div id="bk5" class="bk">喜</div>
	<div id="bk6" class="bk">个</div>
	<div id="bk7" class="bk">换</div>
	<?php
if (isset($_COOKIE['bkgcolor']) && $_COOKIE['bkgcolor'] != $_COOKIE['bkgcolordone']) {
    echo "<form action='updatebkgcolor.php' mathod='post'><input type='submit' style='float:right;' value='保存背景颜色'></form>";
}
if ($_COOKIE['admin'] == 1) {
    echo "<h2 id='titlename'>&nbsp;&nbsp;&nbsp;管理员" . $_COOKIE['user'] . "的主页</h2>
		<a href='admin.php' class='adminlink'>&nbsp;&nbsp;&nbsp;管理其他用户</a><br>";
} else {
    echo "<h2 id='titlename'>&nbsp;&nbsp;&nbsp;" . $_COOKIE['user'] . "的主页</h2><br>";
}
?>
	<a href='index.php' class="logoutlink">注销</a>
	<br>
	<div class="publish">
		<form action="publish.php" method="post">
			<input class="pub-input" placeholder="200字限制" type="text" id="publishtext" name="neirong">
			<input type="button" id="publishbutton" value="发表">
		</form>
	</div>
	<br>
<?php
if (isset($_GET['page'])) {
    $page = $_GET['page'];
} else {
    $page = 1;
}
require_once("tools.php");
$con        = new con_db();
$sql        = "SELECT * FROM liuyan WHERE del != 1 ORDER BY time DESC";
$result     = $con->query($sql);
$totalnum   = $result->num_rows;
//设置每页显示;
$perpagenum = 10;
$totalpage  = ceil($totalnum / $perpagenum);
$perpagebar = 5;

echo "<div style='border:solid white 0px;width:80%;margin:0 auto;background-color:white;'>";

if ($perpagenum * $page > $totalpage * $perpagenum || $page < 1)
    echo "<script>alert('pages out!');window.location.href='homepage.php';</script>";
else {
    mysqli_data_seek($result, $perpagenum * ($page - 1));
    $j               = 0;
    $countvisiblenum = 1;
    while ($j < $perpagenum && $row = $result->fetch_assoc()) {
        echo "<div class='liuyan' style='border-bottom:solid #6950a1 3px;width:100%;margin:0 auto;background-color:white;'>";
        echo "<p>&nbsp;<span>" . $row['id'] . "</sapn>:</p>";
        echo "<p>&nbsp;&nbsp;&nbsp;&nbsp;" . $row['neirong'] . "</p>";
        echo "<p style='float:right;'>" . $row['time'] . "&nbsp;</p>";
        $sql         = "SELECT * FROM reply ORDER BY time";
        $resultreply = $con->query($sql);
        while ($rowreply = $resultreply->fetch_assoc()) {
            if ($row['time'] == $rowreply['timematch']) {
                echo "<br><p>&nbsp;&nbsp;&nbsp;<span>" . $rowreply['id'] . "的回复:</span>&nbsp;" . $rowreply['reply'] . "</p>";
                echo "<p style='float:right;'>" . $rowreply['time'] . "&nbsp;</p>";
            }
        }
        if ($row['id'] == $_COOKIE['user'] || $_COOKIE['admin'] == 1) {
            echo "<form action='delete.php' method='post'><input type='text' name='time' style='display:none;' value='" . $row['time'] . "'><input type='text' name='page' style='display:none;' value='" . $page . "'><input type='submit' style='float:right;height:23px;margin-top:4px;margin-right:5px;' value='删除'></form>";
        }
        echo "<form action='reply.php' method='post'><input type='text' placeholder='200字限制' name='reply' id='replytext" . $countvisiblenum . "' style='float:right;height:19px;margin-top:4px;margin-right:5px;display:none;' >
			<input type='text' name='time' style='display:none;' value='" . $row['time'] . "'>
			<input class='replybutton' type='button' id='replybutton" . $countvisiblenum . "' value='回复'></form><br><br>";
        echo "</div>";
        $j++;
        $countvisiblenum++;
    }
}
echo "</div>";

//copy

echo "<br><div style='display:inline;'><div id='piece' style='width:125px;height:25px;float:left;' ></div>";
if ($page != 1)
    echo "<a href='homepage.php?page=1' class='pages page_button'>首页</a>";
if ($page > 1) {
    echo "<a href='homepage.php?page=" . ($page - 1) . "' class='pages page_button'>上一页</a>";
} else {
    echo "<a href='javascript:void(0);' class='pagesvoid0 page_button' >上一页</a>";
}
$k = $page;
if ($page == 1) //2
    {
    echo "<a href='javascript:void(0);' class='pagesvoid0 page_button' >" . $k . "</a>";
    for ($k++; $k < ($perpagebar + $page) && $k <= $totalpage; $k++) {
        echo "<a href='homepage.php?page=" . $k . "' class='pages page_button'>" . $k . "</a>";
    }
} else if ($page == 2) {
    echo "<a href='homepage.php?page=1' class='pages page_button'>1</a>";
    echo "<a href='javascript:void(0);' class='pagesvoid0 page_button' >" . $k . "</a>";
    for ($k++; $k < ($perpagebar + $page) - 1 && $k <= $totalpage; $k++) {
        echo "<a href='homepage.php?page=" . $k . "' class='pages page_button'>" . $k . "</a>";
    }
} //3
else if ($page == $totalpage) {
    if ($totalpage == 3) {
        $k++;
        $k++;
    } //avoid bug of totalpage3 page3
    if ($totalpage == 4) {
        $k++;
    } //avoid bug of totalpage4 page3
    for ($k = $k - 4; $k < ($perpagebar + $page) - 5 && $k <= $totalpage; $k++) {
        echo "<a href='homepage.php?page=" . $k . "' class='pages page_button'>" . $k . "</a>";
    }
    echo "<a href='javascript:void(0);' class='pagesvoid0 page_button' >" . $k . "</a>";
    for ($k++; $k < ($perpagebar + $page) - 2 && $k <= $totalpage; $k++) {
        echo "<a href='homepage.php?page=" . $k . "' class='pages page_button'>" . $k . "</a>";
    }
} else if ($page == $totalpage - 1) {
    if ($totalpage == 4) {
        $k++;
    } //avoid bug of totalpage4 page3
    for ($k = $k - 3; $k < ($perpagebar + $page) - 5 && $k <= $totalpage; $k++) {
        echo "<a href='homepage.php?page=" . $k . "' class='pages page_button'>" . $k . "</a>";
    }
    $k = $page;
    echo "<a href='javascript:void(0);' class='pagesvoid0 page_button' >" . $k . "</a>";
    for ($k++; $k < ($perpagebar + $page) - 2 && $k <= $totalpage; $k++) {
        echo "<a href='homepage.php?page=" . $k . "' class='pages page_button'>" . $k . "</a>";
    }
} else {
    for ($k = $k - 2; $k < ($perpagebar + $page) - 5 && $k <= $totalpage; $k++) {
        echo "<a href='homepage.php?page=" . $k . "' class='pages page_button'>" . $k . "</a>";
    }
    echo "<a href='javascript:void(0);' class='pagesvoid0 page_button' >" . $k . "</a>";
    for ($k++; $k < ($perpagebar + $page) - 2 && $k <= $totalpage; $k++) {
        echo "<a href='homepage.php?page=" . $k . "' class='pages page_button'>" . $k . "</a>";
    }
}
if ($page < $totalpage) {
    echo "<a href='homepage.php?page=" . ($page + 1) . "' class='pages page_button'>下一页</a>";
} else {
    echo "<a href='javascript:void(0);' class='pagesvoid0 page_button' >下一页</a>";
}
if ($page != $totalpage)
    echo "<a href='homepage.php?page=" . $totalpage . "' class='pages page_button'>尾页</a>";
echo "&nbsp;<input type='text' name='targetpage' placeholder='页码' class='jump'>";
echo "&nbsp;<input type='button' value='跳转' class='jumpbutton'>";
echo "&nbsp;总共: " . $totalpage . "页";
?>
	</div>
	<div class="copyright">
		Copyright@ BUCT SNC WXK
		<br>
		2018
	</div>
	<?php
if (isset($_COOKIE['bkgcolordone'])) {
    echo "<script type='text/javascript'>$(document).ready(function (){changes" . $_COOKIE['bkgcolor'] . "();})</script>";
} else {
    echo "<script type='text/javascript'>$(document).ready(function (){changes" . $_COOKIE['bkgcolor'] . "();})</script>";
}
?> 
<style type="text/css">
 @media screen and (max-width: 770px) { 
    #piece{
        display: none;
    }
} 
</style>
</body>
</html>